package com.ai.aiagent.advisor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.model.MessageAggregator;
import reactor.core.publisher.Flux;

@Slf4j
public class MyloveAdvisor implements CallAroundAdvisor , StreamAroundAdvisor {


    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    @Override
    public int getOrder() {
        return 0;
    }

    private AdvisedRequest befor(AdvisedRequest advisedRequest) {
        log.info("AI request{}",advisedRequest.userText());
        return advisedRequest;
    }


    private void observeAfter (AdvisedResponse advisedResponse) {
        log.info("AI response {}",advisedResponse.response().getResult().getOutput().getText());
    }


    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        AdvisedRequest advisedRequest1 = this.befor(advisedRequest);
        AdvisedResponse advisedResponse = chain.nextAroundCall(advisedRequest1);
        this.observeAfter(advisedResponse);
        return advisedResponse;
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
         advisedRequest = this.befor(advisedRequest);
        Flux<AdvisedResponse> advisedResponseFlux = chain.nextAroundStream(advisedRequest);
        return (new MessageAggregator()).aggregateAdvisedResponse(advisedResponseFlux,this::observeAfter);

    }
}
